define(['jquery', 'kkpager', 'layer_pc', '_orderDetail'], function ($, kkpager, layer,_od) {
    function createProductsHtml(data) {
        var $orderListTBody = $("#orderListTBody");
        var tHtml = '';
        if (!data.msg) {
            $.each(data, function (i, item) {
                tHtml += '<tr>' +
                    '<td>' + (i + 1) + '</td>' +
                    '<td>' + item.OrderNo + '</td>' +
                    '<td>' + item.OrderTime + '</td>' +
                    '<td>' + '<span class="money">￥' + item.Total + '</span>' + '</td>' +
                    '<td>' + ((item.PayTime != null) ? '<span class="text-success">已确认</span>' : '<span class="text-danger">未确认</span>') + '</td>' +
                    '<td>' + ((item.IsCancel == 1) ? '<span class="invalid">作废订单</span>' : '<span class="normal">正常订单</span>') + '</td>' +
                    '<td><a class="am-btn order-detail am-btn-success am-btn-xs" href="javascript:void(0);" data-orderNo="' + item.OrderNo + '">查看明细</a></td>' +
                    '</tr>';
            });
        } else {
            tHtml += '<tr><td colspan="7" style="text-align:center;height:100px;line-height:100px;">' + data.msg + '</td></tr>';
        }
        $orderListTBody.html(tHtml);
        readOrderDetail();
        layer.closeAll();
    }
    function readOrderDetail() {
        var $orderListTable = $("#orderListTable");
        $orderListTable.off("click").on("click", ".order-detail", function () {
            var $this = $(this);
            _od.init({ uid: $("#currentUserID").val(), orderNo: $this.attr("data-orderNo") });
        })
    }

    function initPager(opts) {
        if (!!opts.hasKKpager) {
            opts.total = opts.totalPage;
            opts.pno = opts.pageNo;
            kkpager.refresh(opts);
            return false;
        }
        //  总页数
        var totalPage = opts.totalPage;
        //  页面打开时显示第几页
        var pageNo = opts.pageno;
        //生成分页
        //  有些参数是可选的，比如lang，若不传有默认值
        kkpager.generPageHtml({
            pno: pageNo,
            //总页码
            total: totalPage,
            mode: 'click',//默认值是link，可选link或者click
            click: function (n) {
                opts.pageno = n;
                opts.hasKKpager = false;
                init(opts);
                //手动选中按钮
                this.selectPage(n);
                return false;
            },
            isShowCurrPage: false,
            isShowTotalPage: false,
            isGoPage: false,
            lang: {
                prePageText: '上一页',
                prePageTipText: '上一页',
                nextPageText: '下一页',
                nextPageTipText: '下一页'
            }
        });
    }

    function init(opts) {
        var $kkpager = $("#kkpager");
        var $pagerTotalpage = $("#pagerTotalpage");
        var initIndex = layer.load(1, {
            shade: [0.65, '#000']
        });
        $.ajax({
            type: "get",
            cache: false,
            async: true,
            contentType: "application/json",
            url: "/Goods/Orders",
            data: opts,
            dataType: "json",
            success: function (data) {
                if (!data.success) {
                    createProductsHtml({ msg: data.message })
                    return;
                }
                if (!!data.result.DList && data.result.DList.length > 0) {
                    createProductsHtml(data.result.DList);
                    if (opts.isResetTotalPage) {
                        $pagerTotalpage.val(data.result.PageTotal);
                        opts.isResetTotalPage = false;
                    }
                    if (parseInt($pagerTotalpage.val()) > 1) {
                        $kkpager.show();
                        initPager($.extend(opts, { totalPage: $pagerTotalpage.val(), pageNo: data.result.PageNo }));
                    } else {
                        $kkpager.hide();
                    }
                } else {
                    $kkpager.hide();
                    createProductsHtml({ msg: "未查询到相关数据！" });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("goodsList error：" + "status=" + XMLHttpRequest.status + " && text=" + textStatus);
                layer.closeAll();
            }
        });
    }
    return {
        init: init
    }
});